import time
from .walmart_api import WalmartAPI  # 导入WalmartAPI类

def create_product_data(sku, product_name, price, inventory, product_details, images):
    """创建符合沃尔玛API的产品数据结构"""
    product = {
        "sku": sku,
        "product": {
            "productName": product_name,
            "productType": "GENERAL",
            "isPrimaryImageSizeRequired": True,
            "brandName": product_details.get("brand", ""),
            "shortDescription": product_details.get("short_description", ""),
            "longDescription": product_details.get("long_description", ""),
            "manufacturer": product_details.get("manufacturer", ""),
            "manufacturerPartNumber": product_details.get("mpn", ""),
            "mainImageUrl": images[0] if images else "",
            "productSecondaryImageURL": images[1:] if len(images) > 1 else [],
            "model": product_details.get("model", ""),
            "categoryPath": product_details.get("category", "")
        },
        "price": {
            "currency": "USD",
            "amount": price
        },
        "fulfillment": {
            "fulfillmentLagTime": product_details.get("lag_time", 1),
            "shipMethod": product_details.get("ship_method", "Standard"),
            "shipRegion": product_details.get("ship_region", "United States")
        },
        "inventory": {
            "quantity": inventory,
            "fulfillmentCenterId": product_details.get("fulfillment_center", "DEFAULT")
        }
    }
    
    # 添加可选的产品特性
    if "attributes" in product_details:
        product["product"]["attributes"] = product_details["attributes"]
    
    return product

def main():
    # 替换为您的认证信息
    client_id = "0b05fa33-3e2b-4b9f-9d2d-b3cf647f56f7"
    client_secret = "c-XIcBx4l0mu0Pjs9Sd-7nb1n6tSzfxW2dAnsbfWcaKoXZ7jQu3h-APfYuj744C7d8u8t-O4jYxH3Wj8UqeCPQ"
    
    # 创建API客户端
    walmart_api = WalmartAPI(client_id, client_secret)
    
    # 准备产品数据
    sample_product = create_product_data(
        sku="SKU12345",
        product_name="高品质蓝牙耳机",
        price=29.99,
        inventory=100,
        product_details={
            "brand": "品牌名称",
            "short_description": "超长续航蓝牙耳机，适用于所有移动设备",
            "long_description": "这款蓝牙耳机提供卓越的音质和长达48小时的电池续航...",
            "manufacturer": "制造商名称",
            "mpn": "MPN12345",
            "model": "Model-X123",
            "category": "Electronics/Headphones/Bluetooth Headphones",
            "lag_time": 1,
            "ship_method": "Standard",
            "attributes": [
                {"name": "color", "value": "Black"},
                {"name": "batteryLife", "value": "48 hours"}
            ]
        },
        images=[
            "https://example.com/images/headphone1.jpg",
            "https://example.com/images/headphone2.jpg"
        ]
    )
    
    try:
        # 上传产品
        result = walmart_api.upload_product(sample_product)
        print("产品上传成功！")
        print(f"任务ID: {result.get('feedId')}")
        
        # 检查上传状态
        feed_id = result.get('feedId')
        if feed_id:
            time.sleep(5)  # 等待处理
            status = walmart_api.check_feed_status(feed_id)
            print(f"上传状态: {status.get('feedStatus')}")
    
    except Exception as e:
        print(f"产品上传过程中出现错误: {e}")

if __name__ == "__main__":
    main()
